package com.xyzh05.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.service.PlanManager;
import com.xyzh05.model.Plan;
import com.xyzh05.webapp.action.BaseAction;

import java.util.List;

public class PlanAction extends BaseAction implements Preparable {
	private static final long serialVersionUID = -9196610826055721515L;

	private PlanManager planManager;
	private List<Plan> planList;
	private Plan plan;
	private Long id;
	private String query;
	private Integer resultCode;
	private Long vipServiceId;

	
	/**
	 * Grab the entity from the database before populating with request
	 * parameters
	 */
	public void prepare() throws Exception {
	}

	@SuppressWarnings("unchecked")
	public String list() {
		planList = planManager.findQuery(vipServiceId);
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String newPlan() {
		Boolean flag = planManager.existPlan(vipServiceId);
		if (!flag) {
			planList = planManager.findQuery(vipServiceId);
			return "modify";
		}
		return SUCCESS;
	}

	public String delete() {
		planManager.remove(plan.getId());
		saveMessage(getText("plan.deleted"));
		return SUCCESS;
	}

	public String edit() {
		Boolean flag = planManager.existPlan(vipServiceId);
		if (!flag) {
			planList = planManager.findQuery(vipServiceId);
			return "modify";
		} else {
			plan = new Plan();
		}
		return SUCCESS;
	}

	public String save() throws Exception {
		if (planList.size() > 0) {
			try {
				planManager.batchSave(planList, vipServiceId, getCurrentUser());
	    		resultCode = 1;
			} catch (Exception e) {
				resultCode = 0;
				log.warn(e.getMessage());
			}
		}
		return SUCCESS;
	}

	public PlanManager getPlanManager() {
		return planManager;
	}

	public void setPlanManager(PlanManager planManager) {
		this.planManager = planManager;
	}

	public List<Plan> getPlanList() {
		return planList;
	}

	public void setPlanList(List<Plan> planList) {
		this.planList = planList;
	}

	public Plan getPlan() {
		return plan;
	}

	public void setPlan(Plan plan) {
		this.plan = plan;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public Long getVipServiceId() {
		return vipServiceId;
	}

	public void setVipServiceId(Long vipServiceId) {
		this.vipServiceId = vipServiceId;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

}